import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dio/dio.dart';
import 'package:get/get.dart';
import 'package:palmf_hid/common/utils/utils.dart';

import 'error_response_constants.dart';

// 异常
/// 错误处理拦截器
class ErrorInterceptor extends InterceptorsWrapper {
  // 是否有网
  Future<bool> isConnected() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    return connectivityResult != ConnectivityResult.none;
  }

  @override
  Future<void> onError(
      DioException err, ErrorInterceptorHandler handler) async {
    MyLoadingUtil.hideLoading();
    String errMsg = 'unknownError'.tr;
    MyLogUtil().i('$err');
    switch (err.type) {
      case DioExceptionType.connectionTimeout:
        errMsg = 'connectionTimeout'.tr;
        break;
      case DioExceptionType.sendTimeout:
        errMsg = 'sendRequestTimeout'.tr;
        break;
      case DioExceptionType.receiveTimeout:
        errMsg = 'acceptResponseTimeout'.tr;
        break;
      case DioExceptionType.badCertificate:
        errMsg = 'certificateError'.tr;
        break;
      case DioExceptionType.badResponse:
        int statusCode = err.response?.statusCode ?? 0;
        switch (statusCode) {
          // 参数不符合规范
          case ErrorResponseConstants.PARAM_INVALID:
            errMsg =
                "${'requestFailed'.tr}:${ErrorResponseConstants.PARAM_INVALID}";
            break;
          //缺失必须参数
          case ErrorResponseConstants.PARAM_REQUEST:
            errMsg =
                "${'requestFailed'.tr}:${ErrorResponseConstants.PARAM_REQUEST}";
            break;
          //参数个数超过规定
          case ErrorResponseConstants.PARAM_TOO_MANY:
            errMsg =
                "${'requestFailed'.tr}:${ErrorResponseConstants.PARAM_TOO_MANY}";
            break;
          //当日注册已达上限
          case ErrorResponseConstants.REGIST_LIMIT_TODAY:
            errMsg =
                "${'registrationFailure'.tr}:${ErrorResponseConstants.REGIST_LIMIT_TODAY}";
            break;
          //请求路径必须带参数token
          case ErrorResponseConstants.TOKEN_EMPTY:
            errMsg =
                "${'requestFailed'.tr}:${ErrorResponseConstants.TOKEN_EMPTY}";
            break;
          //token未开放使用
          case ErrorResponseConstants.TOKEN_NO_OPEN:
            errMsg =
                "${'requestFailed'.tr},${'pleaseLogOutAndContactStaff'.tr}:${ErrorResponseConstants.TOKEN_NO_OPEN}";
            break;
          //token已过期
          case ErrorResponseConstants.TOKEN_EXPIRE:
            errMsg =
                "${'requestFailed'.tr},${'pleaseTryLoggingInAgain'.tr}:${ErrorResponseConstants.TOKEN_EXPIRE}";
            break;
          //token冲突
          case ErrorResponseConstants.TOKEN_CONFLICT:
            errMsg =
                "${'requestFailed'.tr},${'pleaseTryLoggingInAgain'.tr}:${ErrorResponseConstants.TOKEN_CONFLICT}";
            break;
          //token信息验证无效
          case ErrorResponseConstants.TOKEN_INVALID:
            errMsg =
                "${'requestFailed'.tr},${'pleaseTryLoggingInAgain'.tr}:${ErrorResponseConstants.TOKEN_INVALID}";
            break;
          //请求太频繁
          case ErrorResponseConstants.REQUEST_TOO_FREQUENTLY:
            errMsg =
                "${'requestFailed'.tr}:${ErrorResponseConstants.REQUEST_TOO_FREQUENTLY}";
            break;
          //非法请求地址
          case ErrorResponseConstants.URL_INVALID:
            errMsg =
                "${'requestFailed'.tr}:${ErrorResponseConstants.URL_INVALID}";
            break;
          //token解码信息错误
          case ErrorResponseConstants.TOKEN_DECODE_FAIL:
            errMsg =
                "${'requestFailed'.tr}:${ErrorResponseConstants.TOKEN_DECODE_FAIL}";
            break;

          default:
            errMsg = 'unableToAccessTheServer'.tr;
        }

        break;
      case DioExceptionType.cancel:
        errMsg = 'requestCancelled'.tr;
        break;
      case DioExceptionType.connectionError:
        errMsg = 'checkNet'.tr;
        MyLogUtil().i("------------------");
        break;
      case DioExceptionType.unknown:
        if (errMsg != 'connectionError'.tr) {
          MyToastUtil.show(errMsg);
        }
        MyLogUtil().i("------------------");
        break;
      default:
        if (errMsg != 'connectionError'.tr) {
          MyToastUtil.show(errMsg);
        }
    }
    MyLogUtil().i("------------------$errMsg");
    if (errMsg != 'connectionError'.tr) {
      MyToastUtil.show(errMsg);
    }
    return handler.next(err);
  }
}
